<template>
  <div class="ratings" ref="ratings">
    <div class="ratings-content">
      <div class="overview">
        <div class="overview-left">
          <h1 class="score">{{info.score}}</h1>
          <div class="title">综合评分</div>
          <div class="rank">高于周边商家 99%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <Star :score="info.serviceScore" :size="36" />
            <span class="score">{{info.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <Star :score="info.foodScore" :size="36" />
            <span class="score">{{info.foodScore}}</span>
          </div>
          <div class="delivery-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{info.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <div class="split"></div>
      <div class="ratingselect">
        <div class="rating-type border-1px">
          <span class="block positive " :class="{active : selectType === 2}" @click="setSelectType(2)">
            全部<span class="count" >{{ratings.length}}</span>
          </span>
          <span class="block positive" :class="{active : selectType === 0}" @click="setSelectType(0)">
            满意<span class="count">{{positiveSize}}</span>
          </span>
          <span class="block negative" :class="{active : selectType === 1}" @click="setSelectType(1)">
            不满意<span class="count">{{ratings.length - positiveSize}}</span>
          </span>
        </div>
        <div class="switch " :class="{on: onlyShowText}" @click="toggleOnlyText">
          <span class="iconfont icon-check"></span>
          <span class="text">只看有内容的评价</span>
        </div>
      </div>
      <div class="rating-wrapper">
        <ul >
          <li class="rating-item" v-for="(rate, index) of filterRatings" :key="index">
            <div class="avatar">
              <img width="28" height="28" :src="rate.avatar">
            </div>
            <div class="content">
              <h1 class="name">{{rate.username}}</h1>
              <div class="star-wrapper">
                <Star :score="rate.score" :size="24" />
                <span class="delivery">{{rate.deliveryTime}}</span>
              </div>
              <p class="text">{{rate.text}}</p>
              <div class="recommend">
                <span class="iconfont " :class="rate.rateType === 0 ? 'icon-hao1' : 'icon-chaping'"></span>
                <span class="item" v-for="(item,index) of rate.recommend" :key="index">{{item}}</span>
              </div>
              <div class="time">{{rate.rateTime | date-format}}</div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Star from '../../../components/star/Star'
import {mapState, mapGetters} from 'vuex'
import BScroll from 'better-scroll'
export default {
  name: 'ShopRatings',
  components: {
    Star,
    BScroll
  },
  data () {
    return {
      onlyShowText: true, // 是否只显示评价内内容
      selectType: 2 // 选择的评价类型： 0满意 1不满意 2全部
    }
  },
  computed: {
    ...mapState(['info', 'ratings']),
    ...mapGetters(['positiveSize']),
    filterRatings () {
      const {ratings, onlyShowText, selectType} = this // 得到相关数据
      // 产生一个过滤新数组
      return ratings.filter(rate => { // filer()是过滤方法
        const {rateType, text} = rate
        /*
         条件1:
           selectType: 0/1/2
           rateType: 0/1
           selectType === rateType || selectType ==== 2
         */
        /* 条件2
         onlyShowText: true/false
         text: 有值/没值
         !onlyShowText || text.length > 0
        */
        return (selectType === 2 || selectType === rateType) && (!onlyShowText || text.length > 0)
      })
    }
  },
  mounted () {
    this.$store.dispatch('getShopRatings', () => { // 与actions中方法getShopRatings对应
      this.$nextTick(() => { // 回调函数必须要有this.$nextTick()来配合
        this.scroll = new BScroll(this.$refs.ratings, {
          click: true
        })
      })
    })
  },
  methods: {
    setSelectType (type) {
      this.selectType = type
    },
    toggleOnlyText () {
      this.onlyShowText = !this.onlyShowText
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "../../../common/stylus/mixins.styl"
  .ratings .rating-wrapper .rating-item .content .text
    margin-top 20px!important
  .ratings
    position: absolute
    top: 395px
    bottom: 0
    left: 0
    width: 100%
    overflow: hidden
    background: #fff
    .overview
      display: flex
      padding: 36px 0
      .overview-left
        flex: 0 0 274px
        padding: 12px 0
        width: 274px
        border-right: 2px solid rgba(7, 17, 27, 0.1)
        text-align: center
        @media only screen and (max-width: 640px)
          flex: 0 0 240px
          width: 240px
        .score
          margin-bottom: 12px
          line-height: 56px
          font-size: 48px
          color: rgb(255, 153, 0)
        .title
          margin-bottom: 16px
          line-height: 24px
          font-size: 24px
          color: rgb(7, 17, 27)
        .rank
          line-height: 20px
          font-size: 20px
          color: rgb(147, 153, 159)
      .overview-right
        flex: 1
        padding: 12px 0 12px 48px
        @media only screen and (max-width: 640px)
          padding-left: 24px
        .score-wrapper
          margin-bottom: 16px
          font-size: 0
          .title
            display: inline-block
            line-height: 36px
            vertical-align: top
            font-size: 24px
            color: rgb(7, 17, 27)
          .star
            display: inline-block
            margin: 0 24px
            vertical-align: top
          .score
            display: inline-block
            line-height: 36px
            vertical-align: top
            font-size: 24px
            color: rgb(255, 153, 0)
        .delivery-wrapper
          font-size: 0
          .title
            line-height: 36px
            font-size: 24px
            color: rgb(7, 17, 27)
          .delivery
            margin-left: 24px
            font-size: 24px
            color: rgb(147, 153, 159)
    .split
      width: 100%
      height: 32px
      border-top: 2px solid rgba(7, 17, 27, 0.1)
      border-bottom: 2px solid rgba(7, 17, 27, 0.1)
      background: #f5f5f5
      box-sizing 2px 2px 2px gray
    .ratingselect
      .rating-type
        padding: 36px 0
        margin: 0 36px
        bottom-border-1px(rgba(7, 17, 27, 0.1))
        font-size: 0
        .block
          display: inline-block
          padding: 16px 24px
          margin-right: 16px
          line-height: 32px
          border-radius: 2px
          font-size: 24px
          color: rgb(77, 85, 93)
          background: rgba(77, 85, 93, 0.2)
          &.active
            background: $green
            color: #fff
          .count
            margin-left: 4px
            font-size: 16px
      .switch
        padding: 24px 36px
        line-height: 48px
        border-bottom: 2px solid rgba(7, 17, 27, 0.1)
        color: rgb(147, 153, 159)
        font-size: 0
        &.on
          .icon-check
            color: $green
        .icon-check
          display: inline-block
          vertical-align: top
          margin-right: 8px
          font-size: 40px
        .text
          display: inline-block
          vertical-align: top
          font-size: 24px
    .rating-wrapper
      padding: 0 36px
      .rating-item
        display: flex
        padding: 36px 0
        border-bottom: 1px solid #dcd6d6
        .avatar
          flex: 0 0 56px
          width: 56px
          margin-right: 24px
          img
            border-radius: 50%
        .content
          position: relative
          flex: 1
          .name
            margin-bottom: 8px
            line-height: 24px
            font-size: 20px
            color: rgb(7, 17, 27)
          .star-wrapper
            margin-bottom: 12px
            font-size: 0
            .star
              display: inline-block
              margin-right: 12px
              vertical-align: top
            .delivery
              display: inline-block
              vertical-align: top
              line-height: 24px
              font-size: 20px
              color: rgb(147, 153, 159)
          .text
            margin-bottom: 16px
            line-height: 36px
            color: rgb(7, 17, 27)
            font-size: 24px
          .recommend
            line-height: 32px
            font-size: 0
            .icon-thumb_up, .icon-thumb_down, .item
              display: inline-block
              margin: 0 16px 8px 0
              font-size: 18px
            .icon-thumb_up
              color: $yellow
            .icon-thumb_down
              color: rgb(147, 153, 159)

            .item
              padding: 0 12px
              border: 2px solid rgba(7, 17, 27, 0.1)
              border-radius: 2px
              color: rgb(147, 153, 159)
              background: #fff
          .time
            position: absolute
            top: 0
            right: 0
            line-height: 24px
            font-size: 20px
            color: rgb(147, 153, 159)
</style>
